package com.lvmama.task.back.job;

import com.lvmama.comm.TaskServiceInterface;
import com.lvmama.comm.pet.po.pub.TaskResult;
import com.lvmama.task.common.service.TaskService;
import com.lvmama.task.common.vo.Task;
import net.sf.json.JSONObject;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by IntelliJ IDEA.<p/>
 * User: troy-kou<p/>
 * Date: 15/1/26<p/>
 * Time: 下午1:53<p/>
 * Email:kouhongyu@163.com<p/>
 *
 * 调度任务日志清理
 */
public class TaskLogCleanupJob implements TaskServiceInterface {

    private static final Log log = LogFactory.getLog(TaskLogCleanupJob.class);
    @Autowired
    private TaskService taskService;

    public TaskResult execute(Long logId, String parameter) throws Exception {

        TaskResult result = new TaskResult();

        JSONObject jsonObject = JSONObject.fromObject(parameter);
        Map<String, Integer> cycleMap = (Map) JSONObject.toBean(jsonObject, Map.class);

        cycleMap = reorganize(cycleMap);
        StringBuffer resultInfo = new StringBuffer();
        int total = 0; //总数
        for (String cycle : cycleMap.keySet()) {
            Map<String, Object> paramMap = new HashMap<String, Object>();
            paramMap.put("_startRow",1);
            paramMap.put("_endRow",10000);
            paramMap.put("cycle", cycle);
            paramMap.put("available", Task.TASK_AVAILABLE.ENABLE);
            List<Task> comTaskList = taskService.getTaskList(paramMap);

            for (Task comTask : comTaskList) {
                Integer count = taskService.cleanupTaskJobLog(comTask.getTaskId(), cycleMap.get(cycle));

                if (count > 0) {
                    resultInfo.append(comTask.getTaskId()).append(",").append(comTask.getTaskName()).append(",").append(count).append(";\n");
                }
                total += count;
            }
        }
        if (resultInfo.length() == 0) {
            resultInfo.append("没有需要清理的日志");
        }else{
            resultInfo.insert(0, "总数:" + total + ";\n");
        }

        result.setResult(resultInfo.toString());
        result.setRunStatus(TaskResult.RUN_STATUS.SUCCESS);

        return result;
    }

    private Map<String, Integer> reorganize(Map<String, Integer> cycleMap) {
        Map<String, Integer> map = new HashMap<String, Integer>();

        for (String cycle : cycleMap.keySet()) {
            if (Task.TASK_CYCLE.SECOND.getCode().equals(cycle) ||
                    Task.TASK_CYCLE.MINUTE.getCode().equals(cycle) ||
                    Task.TASK_CYCLE.HOURS.getCode().equals(cycle) ||
                    Task.TASK_CYCLE.DAY.getCode().equals(cycle) ||
                    Task.TASK_CYCLE.WEEK.getCode().equals(cycle) ||
                    Task.TASK_CYCLE.MONTH.getCode().equals(cycle) ||
                    Task.TASK_CYCLE.YEAR.getCode().equals(cycle) ||
                    Task.TASK_CYCLE.CRON.getCode().equals(cycle)) {

                map.put(cycle, cycleMap.get(cycle));
            }
        }

        return map;
    }

    public void setTaskService(TaskService taskService) {
        this.taskService = taskService;
    }
}
